//
//  LGConsultHomeController.m
//  呼吸训练
//
//  Created by 1234 on 2024/5/8.
//

#import "LGMineViewController.h"
//我的团队
#import "LGTeamManagerController.h"

//更换手机号
#import "LGRevisePhoneController.h"
//设置
#import "LGSettingViewController.h"
//修改密码-验证
#import "LGCheckPasswordController.h"
//修改简介
#import "LGReviseIntroduceController.h"
#import "HXCustomNavigationController.h"

#import "LGDoctorCodeView.h"
#import "LGPhotoWayView.h"
//测试
#import "LGPatientDetailController.h"
@interface LGMineViewController ()<UIImagePickerControllerDelegate,UIAlertViewDelegate,UINavigationControllerDelegate,HXCustomNavigationControllerDelegate>
/**   */
@property (nonatomic , strong)HXPhotoManager *manager;
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)UIImageView *imageView;
/**   */
@property (nonatomic , strong)UIImageView *headPic;
/**   */
@property (nonatomic , strong)UIButton *codeBtn;
/**   */
@property (nonatomic , strong)UIButton *teamBtn;
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIButton *nameBtn;
/**   */
@property (nonatomic , strong)UIButton *hospitalBtn;
/**   */
@property (nonatomic , strong)UIButton *postBtn;
/**   */
@property (nonatomic , strong)UIButton *keshiBtn;
/**   */
@property (nonatomic , strong)UIButton *numberBtn;
/**   */
@property (nonatomic , strong)UIButton *phoneBtn;
/**   */
@property (nonatomic , strong)UIButton *descBtn;
/**   */
@property (nonatomic , strong)UIButton *settingBtn;
/**   */
@property (nonatomic , strong)NSDictionary *dataDic;

@end

@implementation LGMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
    [self requestNurseData];
}

-(void)requestData{
    [RequestUtil withGET:@"/patient/doctorInfo" parameters:@{@"doctorId":kUserId} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            NSDictionary *tempDic = returnData;
            self.dataDic = returnData;
            [self.headPic sd_setImageWithURL:[NSURL URLWithString:LGNSString(tempDic[@"avatar"])] placeholderImage:defaultDoctor];
            [self.nameBtn setTitle:LGNSString(tempDic[@"realName"]) forState:UIControlStateNormal];
            [self.hospitalBtn setTitle:LGNSString(tempDic[@"tenantName"]) forState:UIControlStateNormal];
            [self.postBtn setTitle:LGNSString(tempDic[@"postName"]) forState:UIControlStateNormal];
            [self.keshiBtn setTitle:LGNSString(tempDic[@"deptName"]) forState:UIControlStateNormal];
            [self.numberBtn setTitle:LGNSString(tempDic[@"id"]) forState:UIControlStateNormal];
            NSString *phone = LGNSString(tempDic[@"phone"]);
            self.phoneBtn.selected = (phone.length>0)?NO:YES;
            phone = (phone.length>0)?[phone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"]:@"点击填写";
            [self.phoneBtn setTitle:phone forState:UIControlStateNormal];
            [self.phoneBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:3];
            
            NSString *descStr = LGNSString(tempDic[@"describtion"]);
            self.descBtn.selected = (descStr.length>0)?NO:YES;
            descStr = (descStr.length>0)?descStr:@"点击填写";
            [self.descBtn setTitle:descStr forState:UIControlStateNormal];
            [self.descBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:3];
        }
    } error:^(NSError *error) {
        
    }];
}
  
-(void)requestNurseData{
    [RequestUtil withGET:@"/team/counselor" parameters:@{} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            NSString *nurseId = LGNSString(returnData);
            if ([nurseId isEqualToString:kUserId]) {
                //要隐藏掉团队
                self.teamBtn.hidden = YES;
                self.codeBtn.frame = CGRectMake(Screen_W-viewPix(95), viewPix(25), viewPix(75), viewPix(70));
            }else{
                self.teamBtn.hidden = NO;
                self.codeBtn.frame = CGRectMake(Screen_W-viewPix(170), viewPix(25), viewPix(75), viewPix(70));
            }
        }
    } error:^(NSError *error) {
    
    }];
}

//二维码
-(void)codeBtnAction{
    LGDoctorCodeView *codeView = [[LGDoctorCodeView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    codeView.dataDic = self.dataDic;
    [[ObjectTool mainWindow] addSubview:codeView];
}

//我的团队
-(void)teamBtnAction{
    LGTeamManagerController *controller = [[LGTeamManagerController alloc]init];
    controller.type = LGTeamManagerTypeMine;
    [self.navigationController pushViewController:controller animated:YES];
}

//手机号、简介
-(void)lineButtonAction:(UIButton *)sender{
    if (sender == self.phoneBtn) {
        //更换手机号
        LGRevisePhoneController *controller = [[LGRevisePhoneController alloc]init];
        controller.currentPhone = LGNSString(self.dataDic[@"phone"]);
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if (sender == self.descBtn){
        //修改简介
        LGReviseIntroduceController *controller = [[LGReviseIntroduceController alloc]init];
        controller.content = LGNSString(self.dataDic[@"describtion"]);
        [self.navigationController pushViewController:controller animated:YES];
    }else if (sender == self.settingBtn){
        //设置
        LGSettingViewController *controller = [[LGSettingViewController alloc]init];
        controller.phone = LGNSString(self.dataDic[@"phone"]);
        controller.consultOpen = [self.dataDic[@"persionalAsk"] integerValue]>0?YES:NO;
        [self.navigationController pushViewController:controller animated:YES];
    }
}



//更换头像
-(void)editHeadPicAction{
    LGPhotoWayView *wayView = [[LGPhotoWayView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    [[ObjectTool mainWindow] addSubview:wayView];
    __weak typeof(self) weakSelf = self;
    wayView.LGSelectPhotoWay = ^(NSString * _Nonnull selectWay) {
        if([selectWay isEqualToString:@"camera"]){
            //相机-拍摄
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                UIImagePickerController *pickController = [[UIImagePickerController alloc]init];
                pickController.sourceType = UIImagePickerControllerSourceTypeCamera;
                pickController.delegate = self;
                pickController.allowsEditing = YES;
                pickController.modalPresentationStyle = UIModalPresentationFullScreen;
                [self presentViewController:pickController animated:YES completion:nil];
            }
        }else{
            //相册
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                HXCustomNavigationController *nav = [[HXCustomNavigationController alloc] initWithManager:self.manager delegate:self];
                nav.modalPresentationStyle = UIModalPresentationOverFullScreen;
                nav.modalPresentationCapturesStatusBarAppearance = YES;
                [self presentViewController:nav animated:YES completion:nil];
            }
        }
    };
}

-(void)uploadHeadPic:(UIImage *)image{
    [self resetNavigationTheme];
    [RequestUtil uploadImage:@"/brbot-resource/oss/endpoint/put-file-attach" parameters:@{} header:YES indexName:@"file" imageArray:@[image] success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            __block NSString *link = LGNSString(returnData[@"link"]);
            [RequestUtil bodayPOST:@"/brbot-user/update" parameters:@{@"avatar":LGNSString(returnData[@"link"]),@"id":kUserId} header:YES success:^(NSInteger code, NSString *message, id returnData) {
                if (code == 200) {
                    [self.headPic sd_setImageWithURL:[NSURL URLWithString:link]];
                }else{
                    [TooltipView showMessage:message offset:0];
                }
            } error:^(NSError *error) {
                [TooltipView showMessage:@"上传失败，请重试" offset:0];
            }];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:@"上传失败，请重试" offset:0];
    }];

   
}

//相册选完图片
- (void)photoNavigationViewController:(HXCustomNavigationController *)photoNavigationViewController didDoneAllList:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photoList videos:(NSArray<HXPhotoModel *> *)videoList original:(BOOL)original{
    self.manager = nil;
    NSMutableArray *tempArray = [NSMutableArray array];
    if(photoList.count>0){
        HXPhotoModel *model = photoList[0];
        [self uploadHeadPic:model.previewPhoto];
    }
}


#pragma mark - TZImagePickerController
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        [self uploadHeadPic:image];
    }
}

#pragma mark
#pragma mark ====> 创建控件

-(void)createSubView{
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor colorWithString:@"#F6FAFD"];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(259))];
    imageView.image = [UIImage imageNamed:@"我的-背景"];
    [self.view addSubview:imageView];
    
    UILabel *titleLabel = [UILabel lableWithText:@"我的" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightSemibold) textAlignment:NSTextAlignmentCenter lines:1];
    titleLabel.frame = CGRectMake(0, statusBarHeight, Screen_W, 44);
    [self.view addSubview:titleLabel];
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight-tabBarHeight)];
    self.scrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.scrollView];
    
    self.headPic = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(31), viewPix(25), viewPix(60), viewPix(60))];
    self.headPic.image = [UIImage imageNamed:@"我的-头像"];
    self.headPic.userInteractionEnabled = YES;
    self.headPic.cornerRidus = viewPix(30);
    [self.scrollView addSubview:self.headPic];
    
    UIButton *headButton = [UIButton buttonWithImage:@"我的-照相机" frame:CGRectMake(0, 0, viewPix(60), viewPix(60))];
    headButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    headButton.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
    headButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, viewPix(3), viewPix(8));
    [headButton addTarget:self action:@selector(editHeadPicAction) forControlEvents:UIControlEventTouchUpInside];
    [self.headPic addSubview:headButton];
    
    self.codeBtn = [UIButton buttonWithTitle:@"我的二维码" titleFont:LGFontWeight(12, UIFontWeightMedium) textColor:@"#000000" imageName:@"我的-二维码"];
    self.codeBtn.frame = CGRectMake(Screen_W-viewPix(170), viewPix(25), viewPix(75), viewPix(70));
    [self.codeBtn setButtonInsetsStyle:LGButtonInsetsStyleTop marginSpace:3];
    [self.codeBtn addTarget:self action:@selector(codeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:self.codeBtn];
    
    self.teamBtn = [UIButton buttonWithTitle:@"我的团队" titleFont:LGFontWeight(12, UIFontWeightMedium) textColor:@"#000000" imageName:@"我的-团队"];
    self.teamBtn.frame = CGRectMake(Screen_W-viewPix(95), viewPix(25), viewPix(75), viewPix(70));
    [self.teamBtn setButtonInsetsStyle:LGButtonInsetsStyleTop marginSpace:3];
    [self.teamBtn addTarget:self action:@selector(teamBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:self.teamBtn];
    
    self.baseView = [UIView shadowViewWithColor:@"#E3E0EF" offset:CGSizeMake(0, 0) opacity:1 radius:20];
    self.baseView.frame = CGRectMake(viewPix(16), viewPix(108), Screen_W-viewPix(32), viewPix(464));
    self.baseView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    [self.scrollView addSubview:self.baseView];
    
    self.nameBtn = [self createView:viewPix(8) title:@"姓名" showTip:NO];
    self.hospitalBtn = [self createView:kMaxY(self.nameBtn.frame) title:@"医院" showTip:NO];
    self.postBtn = [self createView:kMaxY(self.hospitalBtn.frame) title:@"职称" showTip:NO];
    self.keshiBtn = [self createView:kMaxY(self.postBtn.frame) title:@"科室" showTip:NO];
    self.numberBtn = [self createView:kMaxY(self.keshiBtn.frame) title:@"工号" showTip:NO];
    self.phoneBtn = [self createView:kMaxY(self.numberBtn.frame) title:@"手机号" showTip:YES];
    self.descBtn = [self createView:kMaxY(self.phoneBtn.frame) title:@"简介" showTip:YES];
    self.settingBtn = [self createView:kMaxY(self.descBtn.frame) title:@"设置" showTip:YES];
}

-(UIButton *)createView:(CGFloat)top title:(NSString *)title showTip:(BOOL)showTip{
    UIButton *titleBtn = [UIButton buttonWithTitle:title titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#2B2A37" imageName:[NSString stringWithFormat:@"我的-%@",title]];
    titleBtn.frame = CGRectMake(viewPix(20), top, viewPix(100), viewPix(56));
    titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    titleBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
    [self.baseView addSubview:titleBtn];
    
    UIButton *button = [UIButton buttonWithTitle:@"" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#2B2A37" frame:CGRectMake(viewPix(120), top, Screen_W-viewPix(136)-viewPix(36), viewPix(56))];
    [button setSelectTitle:@"点击填写" textColor:@"#909399"];
    [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [button addTarget:self action:@selector(lineButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:button];
    if (showTip == YES) {
        [button setImage:[UIImage imageNamed:@"grayTip16"] forState:UIControlStateNormal];
        [button setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
    }
    return button;
}

- (HXPhotoManager *)manager{
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        _manager.configuration.requestImageAfterFinishingSelection = YES;
        _manager.configuration.languageType = HXPhotoLanguageTypeSc;
        _manager.configuration.type = HXConfigurationTypeWXChat;
        _manager.configuration.singleSelected = NO;
        _manager.configuration.photoCanEdit = NO;
        _manager.configuration.maxNum = 1;
    }
    return _manager;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
